import path, { dirname } from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import { defineConfig } from '@rspack/cli';
import { rspack } from '@rspack/core';
import sassembedded from 'sass-embedded';
import { VueLoaderPlugin } from 'vue-loader';
const __dirname = dirname(fileURLToPath(import.meta.url));
const isProduction = process.env.NODE_ENV === 'production';

// Target browsers, see: https://github.com/browserslist/browserslist
const targets = ['chrome >= 87', 'edge >= 88', 'firefox >= 78', 'safari >= 14'];
// console.log(process.env.NODE_ENV);
const getConfig = (css) => {
  if (!css) {
    return {
      loaders: ['style-loader', 'css-loader'],
      experiments: {
        css: false,
      },
      type: 'javascript/auto',
    };
  } else {
    return {
      loaders: [],
      experiments: {
        css: true,
      },
      type: 'css',
    };
  }
};

const customConfig = getConfig(process.env.NODE_ENV == 'production');

export default defineConfig({
  context: __dirname,
  mode: isProduction ? 'production' : 'development',
  entry: {
    main: './src/main.js',
  },
  output: {
    library: {
      name: 'MyLibrary',
      type: 'umd',
      umdNamedDefine: true,
      export: 'default',
    },
    // libraryTarget: 'umd',
    // library: 'vue-demo',
    filename: 'app.[hash].js',
    clean: true,
  },
  resolve: {
    extensions: ['...', '.ts', '.vue', '.scss', '.less', '.css'],
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [
          ...customConfig.loaders,
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: {
                  tailwindcss: {},
                  autoprefixer: {},
                },
              },
            },
          },
        ],
        type: customConfig.type, //
      },
      {
        test: /\.(sass|scss)$/,
        use: [
          ...customConfig.loaders,
          {
            loader: 'sass-loader',
            options: {
              // 同时使用 `modern-compiler` 和 `sass-embedded` 可以显著提升构建性能
              // 需要 `sass-loader >= 14.2.1`
              api: 'modern-compiler',
              implementation: sassembedded,
            },
          },
        ],
        type: customConfig.type, //
      },
      {
        test: /\.less$/i,
        use: [
          ...customConfig.loaders,
          {
            loader: 'less-loader',
          },
        ],
        type: customConfig.type, //
      },
      {
        test: /\.(js|ts)$/,
        use: [
          {
            loader: 'builtin:swc-loader',
            options: {
              mode: 'usage',
              coreJs: '3.26.1',
              jsc: {
                parser: {
                  syntax: 'typescript',
                },
                experimental: {
                  // 配置缓存目录
                  cacheRoot: path.join(__dirname, './node_modules/.cache/swc'),
                },
              },
              env: { targets },
            },
          },
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          experimentalInlineMatchResource: true,
        },
      },
      {
        test: /\.svg/,
        type: 'asset/resource',
      },
    ],
  },
  plugins: [
    new rspack.CopyRspackPlugin({
      patterns: [
        {
          from: path.join(__dirname, 'public'),
          to: 'public',
        },
      ],
    }),
    new rspack.HtmlRspackPlugin({
      template: 'index.html',
      inject: 'body',
      publicPath: './',
      // chunks: ['app'],
    }),
    new rspack.DefinePlugin({
      __VUE_OPTIONS_API__: true,
      __VUE_PROD_DEVTOOLS__: false,
    }),
    new VueLoaderPlugin(),
  ],
  optimization: {
    minimizer: [
      new rspack.SwcJsMinimizerRspackPlugin(),
      new rspack.LightningCssMinimizerRspackPlugin({
        minimizerOptions: { targets },
      }),
    ],
  },
  experiments: {
    css: customConfig.experiments.css,
  },
  externalsType: 'window',
  externals: {
    vue: 'Vue',
  },
});
